package cn.ml.web1662.dao;


import cn.ml.web1662.commons.DBHelp;
import cn.ml.web1662.entity.Pagination;
import cn.ml.web1662.entity.Student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentDaoImpl implements cn.ml.web1662.dao.StudentDao {
    public Student get(int id) {
        String sql = "select * from xueyuan2 where id = ?";
        List params = new ArrayList();
        params.add(id);
        Student student = new Student();
        DBHelp.open();
        DBHelp.executeQuery(sql,params,(rs)->{
            if(rs.next()){
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));
                student.setGender(rs.getString("gender"));
                student.setBornday(rs.getTimestamp("bornday"));
            }
        });
        DBHelp.close();
        return student;
    }

    public void update(Student student) {
        String sql = "update xueyuan2 set name = ? , gender = ? , bornday = ? where id = ?";
        List params = new ArrayList();
        params.add(student.getName());
        params.add(student.getGender());
        params.add(student.getBornday());
        params.add(student.getId());
        DBHelp.open();
        DBHelp.execute(sql,params);
        DBHelp.close();
    }

    public void save(Student student) {
        String sql = "insert into xueyuan2 (name,gender,bornday) values (?,?,?)";
        List params = new ArrayList();
        params.add(student.getName());
        params.add(student.getGender());
        params.add(student.getBornday());
        DBHelp.open();
        DBHelp.execute(sql,params);
        DBHelp.executeQuery("select LAST_INSERT_ID() as id ",null,rs -> {
            if(rs.next()){
                student.setId(rs.getInt("id"));
            }
        });
        DBHelp.close();
    }

    public List<Student> list() {
        String sql = "select * from xueyuan2";
        List<Student> students = new ArrayList<>();
        DBHelp.open();
        DBHelp.executeQuery(sql,null,rs -> {
            while (rs.next()){
                students.add(new Student(
                   rs.getInt("id"),
                   rs.getString("name"),
                   rs.getString("gender"),
                   rs.getTimestamp("bornday")
                ));
            }
        });
        DBHelp.close();
        return students;
    }

    public void delete(int id) {
        String sql= "delete from xueyuan2 where id = ?";
        List params = new ArrayList();
        params.add(id);
        DBHelp.open();
        DBHelp.execute(sql,params);
        DBHelp.close();
    }

    public void listByPagination(Pagination<Student> pagination) {
        //count  result
        String sql1 = "select count(1) from xueyuan2";
        String sql2 = "select * from xueyuan2 limit "+(pagination.getCurrentPageIndex()-1)*pagination.getPageSize()+","+pagination.getPageSize();
        DBHelp.open();
        DBHelp.executeQuery(sql1,null,rs -> {
            if(rs.next()){
                pagination.setTotalCount(rs.getInt(1));
            }
        });
        DBHelp.executeQuery(sql2,null,rs -> {
            List<Student> students = new ArrayList<>();
            while (rs.next()){
                students.add(new Student(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("gender"),
                        rs.getTimestamp("bornday")
                ));
            }
            pagination.setResult(students);
        });
        DBHelp.close();
    }

    public boolean findByName(String name) {
        String sql = "select 1 from xueyuan2 where name = ?";
        List params = new ArrayList();
        params.add(name);
        DBHelp.open();
        Map<String,Boolean> m = new HashMap<>();
        DBHelp.executeQuery(sql,params,rs -> {

            if(rs.next()){
                m.put("flag",true);
            }else {
                m.put("flag",false);
            }

        });
        DBHelp.close();
        return m.get("flag");

    }
}
